package com.luyishi.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.luyishi.entity.Book;
import com.luyishi.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Lenovo
 * @Date: 2020/9/1 23:51
 */
@Controller
public class BookController {

    @Autowired
    private IBookService bookService;

    @GetMapping("/manager/book/add")
    public ModelAndView toAddBook(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("function", "add");
        modelAndView.setViewName("book/book_edit");
        return modelAndView;
    }

    @PostMapping("/manager/book/edit")
    @ResponseBody
    public String addBook(Book book){
        int row = bookService.addBook(book);
        String result = "false";
        if (row > 0){
           result = "true";
        }
        return result;
    }

    @GetMapping("/manager/book/update")
    public ModelAndView toUpdateBook(int bId){
        ModelAndView modelAndView = new ModelAndView();
        Book book= bookService.queryBookById(bId);
        modelAndView.addObject("book", book);
        modelAndView.addObject("function", "update");
        modelAndView.setViewName("book/book_edit");
        return modelAndView;
    }

    @PutMapping("/manager/book/edit")
    @ResponseBody
    public Book updateBook(Book book){
        Book updateBook = bookService.updateBook(book);
        return updateBook;
    }

    @GetMapping("/book/all")
    @ResponseBody
    public PageInfo<Book> queryAllBook(@RequestParam(value = "page", defaultValue = "1") int page){
        PageHelper.startPage(page, 20);
        List<Book> books = bookService.queryAllBook();
        PageInfo<Book> bookPageInfo = new PageInfo<Book>(books, 5);
        return bookPageInfo;
    }

    @GetMapping("/book/search/byname")
    @ResponseBody
    public PageInfo<Book> queryBookByName(@RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "keyword", required = false) String bookName){
        PageHelper.startPage(page, 20);
        List<Book> books = bookService.queryBookByName(bookName);
        PageInfo<Book> bookPageInfo = new PageInfo<Book>(books, 5);
        return bookPageInfo;
    }

    @GetMapping("/book/search/byauthor")
    @ResponseBody
    public PageInfo<Book>  queryBookByAuthor(@RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "keyword", required = false)String author){
        PageHelper.startPage(page, 20);
        List<Book> books = bookService.queryBookByAuthor(author);
        PageInfo<Book> bookPageInfo = new PageInfo<Book>(books, 5);
        return bookPageInfo;
    }

    @GetMapping("/book/search/bybId")
    @ResponseBody
    public List<Book> queryBookByAuthor(int page, int count, int bId){
        List<Book> books = null;

        return books;
    }

}
